﻿using System;
using System.Collections.Generic;
using System.Text;
using log4net.Config;
using System.Xml;
using System.Reflection;
using System.IO;

namespace Barbar.UrlGuard.BL.Concrete {
  internal class Logger : ILogger {
    private log4net.ILog m_Log;
    
    static Logger() {
      Assembly assembly = Assembly.GetExecutingAssembly();
      string config;
      using (Stream stream = assembly.GetManifestResourceStream("Barbar.UrlGuard.Resources.log4net.xml")) {
        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) {
          config = reader.ReadToEnd();
        }
      }

      string folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "WebMonitor");
      string logFile = Path.Combine(folder, "log.txt");
      config = config.Replace("{LogFile}", logFile);

      byte[] buffer = Encoding.UTF8.GetBytes(config);
      using (MemoryStream stream = new MemoryStream(buffer)) {
        log4net.Config.XmlConfigurator.Configure(stream);
      }
    }

    public Logger() {
      m_Log = log4net.LogManager.GetLogger(typeof(Logger));
    }

    #region ILogger Members

    public void Info(object message) {
      m_Log.Info(message);
    }

    public void InfoFormat(string format, params object[] args) {
      m_Log.InfoFormat(format, args);
    }

    public void Fatal(object message, Exception exception) {
      m_Log.Fatal(message, exception);
    }

    #endregion
  }
}
